package com.eascs.bankloan.controller;

import java.text.DecimalFormat;
import java.text.Format;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.bankloan.service.O2oBankLoanService;
import com.eascs.notice.controller.NoticeBaseMasterController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;

@Controller
@RequestMapping("/bankloan")
public class O2oBankLoanController extends NoticeBaseMasterController {

	@Autowired
	O2oBankLoanService bankLoanService;
	
	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "bankloan");
	}

	@RequestMapping("/index")
	public String findBankLoan(String userName) {
		StringBuffer uName = new StringBuffer();
		String[] names = {};
		String name = "";
		if (null != userName && !userName.trim().isEmpty()) {
			if (userName.indexOf(",") != -1 || userName.indexOf("，") != -1 || userName.indexOf(" ") != -1) {
				if (userName.indexOf(",") > 0) {
					names = userName.split(",");
				} else if (userName.indexOf("，") > 0) {
					names = userName.split("，");
				} else if (userName.indexOf(" ") > 0) {
					names = userName.split(" ");
				}
				for (int i = 0; i < names.length; i++) {
					uName.append("'" + names[i] + "',");
				}
				name = uName.substring(0, uName.length() - 1);
			} else {
				name = "'" + userName + "'";
			}
		}
		PageResult<Object[]> pageResult = bankLoanService.findByOaAndO2oPage(name);
		String[] titles = { "贷款单号", "放款银行", "借款人姓名", "借款人证件号", "预批复额度", "放款金额", "贷款类型", "经营平台" };
		SelectDataOption[] selectDataOptions = { null, new SelectDataOption("BANKTYPEO2O"), null, null, null, null, new SelectDataOption("OTO_LOAN_TYPE"), null };
		Button[] titleButtons = new Button[] { ButtonHelper.createOpenButton(null, "/bankloan/detail?billCode=", null) };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(0);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildSelectBox(true);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setFormats(new Format[] { null, null, null, null, new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), null, null });
		// String oaurl = "/rbankloan/oa?oaIndex=";
		// String o2ourl = "/rbankloan/o2o";
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("userName", userName);
		// ViewContext.put("oaurl", oaurl);
		return "bankloan/bankloan";
	}

	@Value("${oa.index.html}")
	String oaUrl;

	@RequestMapping("/detail")
	public String detial(String billCode) {
		if (billCode.indexOf("O2O") != -1)
			return "redirect:" + oaUrl;
		else {
			String loanapplyNo = bankLoanService.getLoanapplyNo(billCode);
			return "redirect:/detail?loanapplyNo=" + loanapplyNo;
		}
	}

}
